﻿using Inovout.Kuajing.Exports.Models;
using Inovout.Kuajing.Exports.Services;
using Inovout.Kuajing.Lottery.Models;
using Inovout.Locations;
using Inovout.Memberships;
using Inovout.Memberships.Models;
using Inovout.Memberships.Services;
using Inovout.NameCodeDictionary;
using Inovout.Organizations.Models;
using Inovout.Organizations.Services;
using Inovout.People.Models;
using Inovout.People.Services;
using Inovout.Security;
using Inovout.Services;
using Inovout.Web;
using System;
using System.Collections.Generic;

namespace Inovout.Kuajing.Exports.Console.Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : WebApplication
    {
        public override void Run()
        {
            base.Run();
            CreateDataBase();
            InitData();
            //InitTestData();
            InitImportData();

        }

        private void CreateDataBase()
        {
            Role role = new Role();
            User user = new User();
            TraditionalOrder tr = new TraditionalOrder();
            Location location = new Location();
            Active active = new Active();
            new Inovout.Repositories.DatabaseInstaller().InstallSchema();


        }
        //private void InitTestData()
        //{
        //    Grade grade = new Grade();
        //    grade.Name = "一年级";

        //    Student student = new Student();
        //    student.Name = "wangsang";
        //    student.Age = 12;
        //    if (grade.Students != null)
        //    {
        //        grade.Students.Add(student);
        //    }
        //    else
        //    {
        //        grade.Students = new HashSet<Student>();
        //        grade.Students.Add(student);
        //    }
        //    IRepositoryService<Grade> gradeService = ComponentRegistry.Resolve<IRepositoryService<Grade>>();
        //    gradeService.Save(grade);


        //}
        private void InitData()
        {
            MembershipUserService membershipUserService = ComponentRegistry.Resolve<MembershipUserService>();
            SystemUser systemUser = new SystemUser();
            systemUser.Name = Guid.NewGuid().ToString();
            var membershipUser = new MembershipUser(
               new Membership { Name = "3cloud", Email = "huay1@samples.cn", Mobile = "13404127324" },
               systemUser,
               "huay1@samples.cn",
               MembershipUserStatus.Activate);
            membershipUser.IdentifierCode = "b2c79d738e84457faa5c69baf9d9b8c";
            membershipUser.Status = Memberships.MembershipUserStatus.Activate;
            membershipUserService.Save(membershipUser as Inovout.Memberships.Models.MembershipUser);


            //insert orginization

            Location location = new Location();
            location.ZoneCode = 1;
            location.ScopeCode = 1;
            location.Name = "中国";
            location.Parent = null;
            location.EnglishName = "china";

            Location location1 = new Location();
            location1.ZoneCode = 2;
            location1.ScopeCode = 1;
            location1.Name = "美国";
            location1.Parent = null;
            location1.EnglishName = "Amercia";





            //创建城市
            Location nj = new Location();
            nj.ZoneCode = 1;
            nj.ScopeCode = 2;
            nj.Name = "南京";
            nj.Parent = location;
            nj.EnglishName = "nanj";

            Location sz = new Location();
            sz.ZoneCode = 2;
            sz.ScopeCode = 2;
            sz.Name = "苏州";
            sz.Parent = location;
            sz.EnglishName = "suz";

            ILocationService locationService = ComponentRegistry.Resolve<LocationService>();
            // locationService.Save(location);
            locationService.Save(location1);
            locationService.Save(nj);
            locationService.Save(sz);



            //创建币种
            NameCodeCollection nameCodeCollectionCurrence = new NameCodeCollection();
            nameCodeCollectionCurrence.Name = "币种";
            nameCodeCollectionCurrence.Scope = "跨境";
            nameCodeCollectionCurrence.Version = "v1";
            nameCodeCollectionCurrence.IsValid = true;


            NameCode nameCodeAmercia = new NameCode();
            nameCodeAmercia.Collection = nameCodeCollectionCurrence;
            nameCodeAmercia.Name = "美元";
            nameCodeAmercia.ShortName = "美元";
            nameCodeAmercia.Code = "1";

            NameCode nameCodeChina = new NameCode();
            nameCodeChina.Collection = nameCodeCollectionCurrence;
            nameCodeChina.Name = "人民币";
            nameCodeChina.ShortName = "人民币";
            nameCodeChina.Code = "2";


            //创建价格条款
            NameCodeCollection nameCodeCollectionRule = new NameCodeCollection();
            nameCodeCollectionRule.Name = "价格条款";
            nameCodeCollectionRule.Scope = "跨境";
            nameCodeCollectionRule.Version = "v1";
            nameCodeCollectionRule.IsValid = true;


            NameCode nameCodeFOB = new NameCode();
            nameCodeFOB.Collection = nameCodeCollectionRule;
            nameCodeFOB.Name = "FOB";
            nameCodeFOB.ShortName = "FOB";
            nameCodeFOB.Code = "1";

            NameCode nameCodeCIF = new NameCode();
            nameCodeCIF.Collection = nameCodeCollectionRule;
            nameCodeCIF.Name = "CIF";
            nameCodeCIF.ShortName = "CIF";
            nameCodeCIF.Code = "2";

            IRepositoryService<NameCode> namecodeService = ComponentRegistry.Resolve<IRepositoryService<NameCode>>();
            namecodeService.Save(nameCodeAmercia);
            namecodeService.Save(nameCodeChina);
            namecodeService.Save(nameCodeFOB);
            namecodeService.Save(nameCodeCIF);


            //创建brander
            Drawer Drawer = new Drawer();
            Drawer.Name = "开三宝";
            Drawer.TaxpayerNo = "TrNo112";
            IRepositoryService<Drawer> drawerService = ComponentRegistry.Resolve<IRepositoryService<Drawer>>();
            drawerService.Save(Drawer);


            //创建商品
            //IRepositoryService<Commodity> commodityService = ComponentRegistry.Resolve<IRepositoryService<Commodity>>();
            //Product product = new Product();
            //product.Brand = "百世杰";
            //product.Name = "衬衫";
            //product.Model = "中号";

            //Commodity commodity = new Commodity();
            //commodity.Product = product;
            //commodityService.Save(commodity);



            //创建电话
            //Phone phone = new Phone();
            //phone.Label = PhoneLabel.Mobile;
            //phone.Value = "13404234567";




            //Phone phone1 = new Phone();
            //phone1.Label = PhoneLabel.Mobile;
            //phone1.Value = "13404234568";


            //Person person = new Person();
            //person.Email = "zhangming@samples.cn";
            //person.Name = "张明";
            ////List<Phone> zmPhone=new  List<Phone>();
            //person.Phones.Add(phone);


            //Person person1 = new Person();
            //person1.Email = "wanghai@samples.cn";
            //person1.Name = "王海";
            ////List<Phone> whPhone = new List<Phone>();
            //person1.Phones.Add(phone1);


            //PhoneService phoneService = ComponentRegistry.Resolve<PhoneService>();
            //phoneService.Create(phone);
            //phoneService.Create(phone1);

            //OrganizationUnit org = new OrganizationUnit() { Name = "南京苏宁", Location = location, Email = "sn@163.com" };
            //OrganizationUnit org1 = new OrganizationUnit() { Name = "南京三宝", Location = location1, Email = "sb@163.com" };
            //OrganizationUnitService organizationUnitService = ComponentRegistry.Resolve<OrganizationUnitService>();
            //organizationUnitService.Create(org);
            //organizationUnitService.Create(org1);

            //phone.Person = person;
            //phone1.Person = person1;

            //Employee employee = new Employee();
            //employee.Person = person;
            //employee.Organization = org;

            //Employee employee1 = new Employee();
            //employee1.Person = person1;
            //employee1.Organization = org1;

            //EmployeeService employeeService = ComponentRegistry.Resolve<EmployeeService>();
            //employeeService.Create(employee);
            //employeeService.Create(employee1);

            ////创建交易方
            ////Consignee consignee = new Consignee();
            ////consignee.Organization = org;
            ////consignee.Address = "仙林大道2号";
            ////ConsigneeService consigneeService = ComponentRegistry.Resolve<ConsigneeService>();
            ////consigneeService.Save(consignee);




        }

        private void InitImportData() 
        {         
            Active active = new Active();
            active.ClickCount = 2;
            active.Name = "6.28公司庆祝";
            active.Enabled = true;
            active.WinningRate = 3;


            Prize fiveYuanPrize = new Prize();
            fiveYuanPrize.Name = "5元红包";
            fiveYuanPrize.Quantity = 10;
            fiveYuanPrize.Active = active;

            Prize tenYuanPrize = new Prize();
            tenYuanPrize.Name = "10元红包";
            tenYuanPrize.Quantity = 10;
            tenYuanPrize.Active = active;

            Prize fiftyphonebill = new Prize();
            fiftyphonebill.Name = "50元话费";
            fiftyphonebill.Quantity = 10;
            fiftyphonebill.Active = active;
            IRepositoryService<Active> activeService = ComponentRegistry.Resolve<IRepositoryService<Active>>();
            activeService.Save(active);

            IRepositoryService<Prize> prizeService = ComponentRegistry.Resolve<IRepositoryService<Prize>>();
            prizeService.Save(fiveYuanPrize);
            prizeService.Save(tenYuanPrize);
            prizeService.Save(fiftyphonebill);


        }

    }
}